﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PraxisRent.Entities;
using PraxisRent.BL;
using Microsoft.Practices.Unity;

namespace PraxisRent.UI
{
    public partial class FrmVencimientoAlquileres : Form
    {
        [Dependency]
        public IAlquilerBL alquilerBL { get; set; }

        [Dependency]
        public IDetalleAlquilerBL detAlquiBL { get; set; }

        [Dependency]
        public IClienteJuridicoBL cliJuridicoBL { get; set; }

        [Dependency]
        public IClienteNaturalBL cliNaturalBL { get; set; }

        [Dependency]
        public ITelefonoClienteBL telfClienBL { get; set; }

        public FrmVencimientoAlquileres()
        {
            InitializeComponent();
            this.BuilUp();
        }

        private void FrmVencimientoAlquileres_Load(object sender, EventArgs e)
        {
            cargaAlquilerPorVencer();
        }

        public void cargaAlquilerPorVencer()
        {
            var a = dtpFecEntrega.Value;
            aLQUILERBindingSource.DataSource = alquilerBL.GetAllFromAlquilerByFechaVencimiento(Convert.ToInt32(a.Day.ToString()));
            if (aLQUILERBindingSource.Count == 0)
            {
                MessageBox.Show("Actualmente no hay Alquileres por Vencer.", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
        }

        private void aLQUILERRadGridView_CurrentCellChanged(object sender, Telerik.WinControls.UI.CurrentCellChangedEventArgs e)
        {
            var currentAlquiler = aLQUILERBindingSource.Current as ALQUILER;

            if (currentAlquiler != null)
            {
                dETALLE_ALQUILERBindingSource.DataSource = detAlquiBL.GetAllFromDetalleAlquilerByAlquilerId(currentAlquiler.Codigo);
                tELEFONO_CLIENTEsBindingSource.DataSource = (from c in telfClienBL.GetAllFromTelefonoCliente()
                                                             where c.CLIENTE.Codigo.Equals(currentAlquiler.CodigoCliente)
                                                             select c).ToList();

                #region Cliente
                try
                {
                    for (int count = 0; count < aLQUILERRadGridView.Rows.Count; count++)
                    {
                        if (currentAlquiler != null)
                        {
                            CLIENTE a = aLQUILERRadGridView.Rows[count].Cells[1].Value as CLIENTE;
                            var cliJuri = (from c in cliJuridicoBL.GetAllFromClienteJuridico()
                                           where c.ClienteJuridicoId.Equals(a.Codigo)
                                           select c).SingleOrDefault();
                            var cliNat = (from c in cliNaturalBL.GetAllFromClienteNatural()
                                          where c.ClienteNaturalId.Equals(a.Codigo)
                                          select c).SingleOrDefault();

                            if (cliJuri != null)
                            {
                                aLQUILERRadGridView.Rows[count].Cells[2].Value = cliJuri.Ruc.ToString() + " - " + cliJuri.RazonSocial.ToString();
                            }
                            else if (cliNat != null)
                            {
                                aLQUILERRadGridView.Rows[count].Cells[2].Value = cliNat.Dni.ToString() + " - " + cliNat.NombreCompleto.ToString();
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Error");
                }
                #endregion
            }
        }
        }
    }

